package com.ruicar.afs.cloud.workflow.greatwall.service.gwt.impl;

import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.security.vo.UserVo;
import com.ruicar.afs.cloud.workflow.api.constant.FlowConstant;
import com.ruicar.afs.cloud.workflow.api.dto.afsprocess.request.*;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.WorkFlowResponse;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.request.*;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.response.FLowNodeUserBean;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.response.FlowCmdBean;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.response.FlowInstanceBean;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.response.FlowNodeBean;
import com.ruicar.afs.cloud.workflow.api.enums.FlowStatus;
import com.ruicar.afs.cloud.workflow.api.util.FlowMappingHelper;
import com.ruicar.afs.cloud.workflow.greatwall.dto.GwtFlowEngineResponse;
import com.ruicar.afs.cloud.workflow.greatwall.dto.engine.*;
import com.ruicar.afs.cloud.workflow.greatwall.entity.AfsGreatWallWorkflowBizData;
import com.ruicar.afs.cloud.workflow.greatwall.entity.AfsGreatWallWorkflowNoticeException;
import com.ruicar.afs.cloud.workflow.greatwall.enums.GwtMsgType;
import com.ruicar.afs.cloud.workflow.greatwall.feign.GwtWorkFLowFeign;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallWorkflowBizDataService;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallWorkflowNoticeExceptionService;
import com.ruicar.afs.cloud.workflow.greatwall.service.gwt.GwtWorkFlowEngineProcess;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import static com.ruicar.afs.cloud.workflow.api.enums.FlowStatus.GIVE_UP;

@Service
@Slf4j
@AllArgsConstructor
public class GwtWorkFlowEngineProcessImpl implements GwtWorkFlowEngineProcess {
    private final GwtWorkFLowFeign gwtWorkFLowFeign;
    private final StringRedisTemplate stringRedisTemplate;
    private final AfsGreatWallWorkflowBizDataService afsGreatWallWorkflowBizDataService;
    private final AfsGreatWallWorkflowNoticeExceptionService afsGreatWallWorkflowNoticeExceptionService;

    private String getServiceId(String afsKey) {
        return FlowMappingHelper.getServiceId(afsKey);
    }

    @Override
    public WorkFlowResponse startFlow(StartFlowRequest startFlowRequest, String bizData, String serviceName) {
        String serviceId = getServiceId(startFlowRequest.getAfsFlowKey());
        stringRedisTemplate.opsForValue().set(FlowConstant.templateKey(serviceId, startFlowRequest.getBizDataId()), bizData,10,TimeUnit.MINUTES);
        AfsGreatWallWorkflowBizData afsGreatWallWorkflowBizData = new AfsGreatWallWorkflowBizData();
        afsGreatWallWorkflowBizData.setBizData(bizData);
        afsGreatWallWorkflowBizData.setBizDataId(startFlowRequest.getBizDataId());
        afsGreatWallWorkflowBizData.setFlowServiceId(serviceId);
        afsGreatWallWorkflowBizData.setAfsServiceName(serviceName);
        afsGreatWallWorkflowBizData.setFlowStatus(FlowStatus.RUNING);
        afsGreatWallWorkflowBizDataService.saveBizDate(afsGreatWallWorkflowBizData);
        try {
            log.info("流程发起 bizDataId={},业务数据{}",startFlowRequest.getBizDataId(),bizData);
            String response = gwtWorkFLowFeign.startFlow(serviceId, startFlowRequest.getBizDataId(), startFlowRequest.getFlowName());
            GwtFlowEngineResponse gwtFlowEngineResponse = JSON.parseObject(response, GwtFlowEngineResponse.class);
            log.info("服务[{}]请求流程引擎,业务主键[{}],引擎返回信息[{}]", serviceName, startFlowRequest.getBizDataId(), gwtFlowEngineResponse);
            if (gwtFlowEngineResponse.getSuccess() != 1) {
                throw new AfsBaseException(StringUtils.isNotEmpty(gwtFlowEngineResponse.getMessage()) ? gwtFlowEngineResponse.getMessage() : "流程发起异常");
            }
            return WorkFlowResponse.builder().code(gwtFlowEngineResponse.getCode()).message(gwtFlowEngineResponse.getMessage()).data(gwtFlowEngineResponse.getData()).success(gwtFlowEngineResponse.getSuccess()).build();
        } catch (Exception e) {
            log.error("流程发起异常", e);
            afsGreatWallWorkflowBizDataService.clearStartData(afsGreatWallWorkflowBizData);
            throw new AfsBaseException(CommonConstants.FAIL, "流程发起异常");
        } finally {
            stringRedisTemplate.delete(FlowConstant.templateKey(serviceId, startFlowRequest.getBizDataId()));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkFlowResponse taskSubmit(TaskSubmitRequest taskSubmitRequest, String bizData, String serviceName) {
        stringRedisTemplate.opsForValue().set(FlowConstant.templateKey(FlowMappingHelper.getServiceId(taskSubmitRequest.getAfsFlowKey()), taskSubmitRequest.getBizDataId()), bizData,10,TimeUnit.MINUTES);
        TaskSubmitDto taskSubmitDto = TaskSubmitDto.builder()
                .cmdId(taskSubmitRequest.getCmdId())
                .cmdMemo(taskSubmitRequest.getRemark())
                .flowInstanceId(taskSubmitRequest.getGwtUserId())
                .nextUserId(taskSubmitRequest.getNextUserName())
                .build();
        try {
            if (afsGreatWallWorkflowBizDataService.exception(FlowMappingHelper.getServiceId(taskSubmitRequest.getAfsFlowKey()), taskSubmitRequest.getBizDataId())) {
                throw new AfsBaseException("流程处于异常状态无法提交");
            }
            log.info("流程提交 bizDataId={},业务数据{}",taskSubmitRequest.getBizDataId(),bizData);
            afsGreatWallWorkflowBizDataService.updateBizData(FlowMappingHelper.getServiceId(taskSubmitRequest.getAfsFlowKey()), taskSubmitRequest.getBizDataId(), serviceName, bizData);
            String response = gwtWorkFLowFeign.taskSubmit(JSONObject.toJSONString(taskSubmitDto));
            GwtFlowEngineResponse gwtFlowEngineResponse = JSON.parseObject(response, GwtFlowEngineResponse.class);
            if (gwtFlowEngineResponse.getSuccess() != 1) {
                throw new AfsBaseException(StringUtils.isNotEmpty(gwtFlowEngineResponse.getMessage()) ? gwtFlowEngineResponse.getMessage() : "路程提交异常");
            }
            return WorkFlowResponse.builder().code(gwtFlowEngineResponse.getCode()).message(gwtFlowEngineResponse.getMessage()).data(gwtFlowEngineResponse.getData()).success(gwtFlowEngineResponse.getSuccess()).build();
        } catch (Exception e) {
            log.error("流程提交异常", e);
            throw new AfsBaseException(CommonConstants.FAIL, e.getMessage());
        } finally {
            stringRedisTemplate.delete(FlowConstant.templateKey(FlowMappingHelper.getServiceId(taskSubmitRequest.getAfsFlowKey()), taskSubmitRequest.getBizDataId()));
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public WorkFlowResponse taskReformist(TaskReformistRequest taskReformistRequest, String bizData, String serviceName) {
        stringRedisTemplate.opsForValue().set(FlowConstant.templateKey(FlowMappingHelper.getServiceId(taskReformistRequest.getAfsFlowKey()), taskReformistRequest.getBizDataId()), bizData);
        TaskReformistDto taskReformistDto = TaskReformistDto.builder()
                .flowInstanceId(taskReformistRequest.getFlowInstanceId())
                .userId(taskReformistRequest.getUserName())
                .userName(taskReformistRequest.getUserRealName())
                .build();
        try {
            if (afsGreatWallWorkflowBizDataService.exception(FlowMappingHelper.getServiceId(taskReformistRequest.getAfsFlowKey()), taskReformistRequest.getBizDataId())) {
                throw new AfsBaseException("流程处于异常状态无法继续");
            }
            log.info("流程移交 bizDataId={},业务数据{}",taskReformistRequest.getBizDataId(),bizData);
            afsGreatWallWorkflowBizDataService.updateBizData(FlowMappingHelper.getServiceId(taskReformistRequest.getAfsFlowKey()), taskReformistRequest.getBizDataId(), serviceName, bizData);
            String response = gwtWorkFLowFeign.taskReformist(taskReformistDto.getFlowInstanceId(), taskReformistDto.getUserId(), taskReformistDto.getUserName());
            GwtFlowEngineResponse gwtFlowEngineResponse = JSON.parseObject(response, GwtFlowEngineResponse.class);
            return WorkFlowResponse.builder().code(gwtFlowEngineResponse.getCode()).message(gwtFlowEngineResponse.getMessage()).data(gwtFlowEngineResponse.getData()).success(gwtFlowEngineResponse.getSuccess()).build();
        } catch (Exception e) {
            log.error("流程改单异常", e);
            throw new AfsBaseException(CommonConstants.FAIL, "流程改单异常");
        } finally {
            stringRedisTemplate.opsForValue().set(FlowConstant.templateKey(FlowMappingHelper.getServiceId(taskReformistRequest.getAfsFlowKey()), taskReformistRequest.getBizDataId()), bizData, 1, TimeUnit.SECONDS);
        }
    }

    @Override
    @Transactional
    public WorkFlowResponse flowGiveUp(FLowGiveUpRequest fLowGiveUpRequest, String serviceName) {
        FlowGiveUpDto flowGiveUpDto = FlowGiveUpDto.builder()
                .flowInstanceId(fLowGiveUpRequest.getFlowInstanceId())
                .reason(fLowGiveUpRequest.getReason())
                .build();
        AfsGreatWallWorkflowBizData afsGreatWallWorkflowBizData = afsGreatWallWorkflowBizDataService.getOne(Wrappers.<AfsGreatWallWorkflowBizData>lambdaQuery().eq(AfsGreatWallWorkflowBizData::getFlowInstanceId, flowGiveUpDto.getFlowInstanceId()));
        if (afsGreatWallWorkflowBizData != null && afsGreatWallWorkflowBizData.getFlowStatus() == GIVE_UP) {
            return WorkFlowResponse.builder().code("1").message("").data("").success(1).build();
        }
        try {
            String response = gwtWorkFLowFeign.flowGiveUp(flowGiveUpDto.getFlowInstanceId(), flowGiveUpDto.getReason());
            GwtFlowEngineResponse gwtFlowEngineResponse = JSON.parseObject(response, GwtFlowEngineResponse.class);
            if (afsGreatWallWorkflowBizData != null && gwtFlowEngineResponse.getSuccess() == 1) {
                afsGreatWallWorkflowBizData.setFlowStatus(GIVE_UP);
                afsGreatWallWorkflowBizDataService.updateById(afsGreatWallWorkflowBizData);
                afsGreatWallWorkflowNoticeExceptionService.remove(
                        Wrappers
                                .<AfsGreatWallWorkflowNoticeException>lambdaUpdate()
                                .eq(AfsGreatWallWorkflowNoticeException::getFlowInstanceId, flowGiveUpDto.getFlowInstanceId())
                );
            }
            return WorkFlowResponse.builder().code(gwtFlowEngineResponse.getCode()).message(gwtFlowEngineResponse.getMessage()).data(gwtFlowEngineResponse.getData()).success(gwtFlowEngineResponse.getSuccess()).build();
        } catch (Exception e) {
            log.error("流程终止异常", e);
            throw new AfsBaseException(CommonConstants.FAIL, "流程终止异常");
        }
    }

    @Override
    public WorkFlowResponse queryCmd(QueryFLowCmdRequest queryFLowCmdRequest, String serviceName) {
        InstanceQueryDto instanceQueryDto = InstanceQueryDto.builder()
                .flowInstanceID(queryFLowCmdRequest.getFlowInstanceId())
                .build();
        try {
            String response = gwtWorkFLowFeign.queryCmd(instanceQueryDto.getFlowInstanceID());
            GwtFlowEngineResponse gwtFlowEngineResponse = JSON.parseObject(response, GwtFlowEngineResponse.class);
            if (gwtFlowEngineResponse.getSuccess() != 1) {
                return WorkFlowResponse.builder().code(gwtFlowEngineResponse.getCode()).message(gwtFlowEngineResponse.getMessage()).data("[]").success(gwtFlowEngineResponse.getSuccess()).build();
            }
            List<FlowCmdBean> list = JSON.parseArray(gwtFlowEngineResponse.getData(), FlowCmdBean.class);
            return WorkFlowResponse.builder().code(gwtFlowEngineResponse.getCode()).afsData(list).message(gwtFlowEngineResponse.getMessage()).data(gwtFlowEngineResponse.getData()).success(gwtFlowEngineResponse.getSuccess()).build();
        } catch (Exception e) {
            log.error("流程改单异常", e);
            throw new AfsBaseException(CommonConstants.FAIL, "查询任务操作失败");
        }
    }

    @Override
    public WorkFlowResponse queryFlowInstance(QueryFlowInstanceRequest queryFlowInstanceRequest, String serviceName) {
        InstanceQueryDto instanceQueryDto = InstanceQueryDto.builder()
                .flowInstanceID(queryFlowInstanceRequest.getFlowInstanceId())
                .build();
        try {

            String response = gwtWorkFLowFeign.queryInstance(instanceQueryDto.getFlowInstanceID());
            GwtFlowEngineResponse gwtFlowEngineResponse = JSON.parseObject(response, GwtFlowEngineResponse.class);
            if (gwtFlowEngineResponse.getSuccess() != 1) {
                return WorkFlowResponse.builder().code(gwtFlowEngineResponse.getCode()).message(gwtFlowEngineResponse.getMessage()).data("{}").success(gwtFlowEngineResponse.getSuccess()).build();
            }
            FlowInstanceBean flowInstanceBean = JSONObject.parseObject(gwtFlowEngineResponse.getData(), FlowInstanceBean.class);
            flowInstanceBean.setAfsFlowKey(FlowMappingHelper.getAfsKey(flowInstanceBean.getServiceId()));
            return WorkFlowResponse.builder().afsData(flowInstanceBean).code(gwtFlowEngineResponse.getCode()).message(gwtFlowEngineResponse.getMessage()).data(gwtFlowEngineResponse.getData()).success(gwtFlowEngineResponse.getSuccess()).build();
        } catch (Exception e) {
            log.error("流程实例查询异常", e);
            throw new AfsBaseException(CommonConstants.FAIL, "流程实例查询异常");
        }
    }

    @Override
    @Transactional
    public WorkFlowResponse taskCallBack(TaskCallBackRequest taskCallBackRequest, String bizData, String serviceName) {
        stringRedisTemplate.opsForValue().set(FlowConstant.templateKey(FlowMappingHelper.getServiceId(taskCallBackRequest.getAfsFlowKey()), taskCallBackRequest.getBizDataId()), bizData);
        TaskCallBackDto taskCallBackDto = TaskCallBackDto.builder()
                .flowInstanceId(taskCallBackRequest.getFlowInstanceId())
                .userId(taskCallBackRequest.getUserName())
                .userName(taskCallBackRequest.getUserRealName())
                .build();
        try {
            if (afsGreatWallWorkflowBizDataService.exception(FlowMappingHelper.getServiceId(taskCallBackRequest.getAfsFlowKey()), taskCallBackRequest.getBizDataId())) {
                throw new AfsBaseException("流程处于异常状态无法继续");
            }
            afsGreatWallWorkflowBizDataService.updateBizData(FlowMappingHelper.getServiceId(taskCallBackRequest.getAfsFlowKey()), taskCallBackRequest.getBizDataId(), serviceName, bizData);
            String response = gwtWorkFLowFeign.taskCallBack(taskCallBackDto.getFlowInstanceId(), taskCallBackDto.getUserId(), taskCallBackDto.getUserName());
            GwtFlowEngineResponse gwtFlowEngineResponse = JSON.parseObject(response, GwtFlowEngineResponse.class);
            return WorkFlowResponse.builder().code(gwtFlowEngineResponse.getCode()).message(gwtFlowEngineResponse.getMessage()).data(gwtFlowEngineResponse.getData()).success(gwtFlowEngineResponse.getSuccess()).build();
        } catch (Exception e) {
            log.error("任务撤回异常", e);
            throw new AfsBaseException(CommonConstants.FAIL, "任务撤回异常");
        } finally {
            stringRedisTemplate.opsForValue().set(FlowConstant.templateKey(FlowMappingHelper.getServiceId(taskCallBackRequest.getAfsFlowKey()), taskCallBackRequest.getBizDataId()), bizData, 1, TimeUnit.SECONDS);
        }
    }


    @Override
    public WorkFlowResponse queryAllNode() {
        try {
            String response = gwtWorkFLowFeign.seatList();
            List<FlowNodeBean> flowNodeBeans = JSONObject.parseArray(response, FlowNodeBean.class);
            flowNodeBeans.forEach(flowNodeBean -> {
                flowNodeBean.setAfsFlowKey(FlowMappingHelper.getAfsKey(flowNodeBean.getServiceId()));
            });
            return WorkFlowResponse.builder().code("1").afsData(flowNodeBeans).message("1").data(response).success(1).build();
        } catch (Exception e) {
            log.error("流程节点查询失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "查询所有节点失败");
        }
    }

    @Override
    public WorkFlowResponse pageQueryNode(Page page) {
        try {
            String response = gwtWorkFLowFeign.pageSeatList(page.getSize(), page.getCurrent());
            JSONObject jsonObject = JSONObject.parseObject(response);
            if (jsonObject.getInteger("success") != 1) {
                return WorkFlowResponse.builder().code("0").afsData(null).message("1").data("{}").success(0).build();
            }
            List<FlowNodeBean> flowNodeBeans = jsonObject.getJSONObject("data").getJSONArray("data").toJavaList(FlowNodeBean.class);
            flowNodeBeans.forEach(flowNodeBean -> {
                flowNodeBean.setAfsFlowKey(FlowMappingHelper.getAfsKey(flowNodeBean.getServiceId()));
            });
            page.setRecords(flowNodeBeans);
            page.setTotal(jsonObject.getJSONObject("data").getLong("recordsTotal"));
            return WorkFlowResponse.builder().code("1").afsData(page).message("1").data(response).success(1).build();
        } catch (Exception e) {
            log.error("分页查询所有节点失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "分页查询所有节点失败");
        }
    }

    @Override
    public WorkFlowResponse pageQueryNodeUser(QueryCondition<QueryNodeUserRequest> queryNodeUserRequestQueryCondition) {
        try {
            String response = gwtWorkFLowFeign.queryNodeUser(queryNodeUserRequestQueryCondition.getPageSize(), queryNodeUserRequestQueryCondition.getPageNumber(), queryNodeUserRequestQueryCondition.getCondition().getNodeId());
            JSONObject jsonObject = JSONObject.parseObject(response);
            if (jsonObject.getInteger("success") != 1) {
                return WorkFlowResponse.builder().code("0").afsData(null).message("1").data("{}").success(0).build();
            }
            List<FLowNodeUserBean> flowNodeBeans = jsonObject.getJSONObject("data").getJSONArray("data").toJavaList(FLowNodeUserBean.class);
//            flowNodeBeans.forEach(flowNodeBean -> {
//                flowNodeBean.setAfsFlowKey(FlowMappingHelper.getAfsKey(flowNodeBean.getServiceId()));
//            });
            Page page = new Page();
            page.setCurrent(queryNodeUserRequestQueryCondition.getPageNumber());
            page.setSize(queryNodeUserRequestQueryCondition.getPageSize());
            page.setRecords(flowNodeBeans);
            page.setTotal(jsonObject.getJSONObject("data").getLong("recordsTotal"));
            return WorkFlowResponse.builder().code("1").afsData(page).message("1").data(response).success(1).build();
        } catch (Exception e) {
            log.error("查询用户失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "查询用户失败");
        }
    }

    @Override
    public WorkFlowResponse addNodeUser(AddNodeUserRequest addNodeUserRequest) {
        try {
            JSONObject request = new JSONObject();
            request.put("taskId", addNodeUserRequest.getNodeId());
            String[] userNames = addNodeUserRequest
                    .getUserVoList()
                    .stream().map(UserVo::getUserRealName)
                    .collect(Collectors.toList())
                    .toArray(new String[0]);
            String[] userIds = addNodeUserRequest
                    .getUserVoList()
                    .stream().map(UserVo::getUsername)
                    .collect(Collectors.toList())
                    .toArray(new String[0]);

            request.put("userNames", ArrayUtil.join(userNames, ","));
            request.put("userIds", ArrayUtil.join(userIds, ","));
            String response = gwtWorkFLowFeign.addNodeUser(request.toJSONString());
            return JSONObject.parseObject(response, WorkFlowResponse.class);
        } catch (Exception e) {
            log.error("增加用户失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "增加用户失败");
        }
    }

    @Override
    public WorkFlowResponse changeTotalWeight(ChangeTotalUserWeightRequest changeTotalUserWeightRequest) {
        try {
            String response = gwtWorkFLowFeign.changeUserTotalWeight(changeTotalUserWeightRequest.getUserName(), changeTotalUserWeightRequest.getWeight());
            return JSONObject.parseObject(response, WorkFlowResponse.class);
        } catch (Exception e) {
            log.error("总权重改变失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "总权重改变失败");
        }
    }

    @Override
    public WorkFlowResponse changeWeight(ChangeUserWeightRequest changeUserWeightRequest) {
        try {
            String[] userIds = changeUserWeightRequest
                    .getGwfUserIds()
                    .toArray(new String[0]);
            String response = gwtWorkFLowFeign.changeUserWeight(ArrayUtil.join(userIds, ","), changeUserWeightRequest.getWeight());
            return JSONObject.parseObject(response, WorkFlowResponse.class);
        } catch (Exception e) {
            log.error("权重改变失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "权重改变失败");
        }
    }

    @Override
    public WorkFlowResponse clearTaskCount(List<String> userIds) {
        try {
            String response = gwtWorkFLowFeign.clearTaskCount(ArrayUtil.join(userIds.toArray(new String[0]), ","));
            return JSONObject.parseObject(response, WorkFlowResponse.class);
        } catch (Exception e) {
            log.error("权重改变失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "权重改变失败");
        }
    }

    @Override
    public WorkFlowResponse removeNodeUsers(List<String> userIds) {
        try {
            String response = gwtWorkFLowFeign.removeNodeUsers(ArrayUtil.join(userIds.toArray(new String[0]), ","));
            return JSONObject.parseObject(response, WorkFlowResponse.class);
        } catch (Exception e) {
            log.error("移除节点处理人失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "移除节点处理人失败");
        }
    }

    @Override
    public WorkFlowResponse moveUserToNode(MoveUserToNewNodeRequest request) {
        try {
            log.info("转组====》{}",JSONObject.toJSONString(request));
            String response = gwtWorkFLowFeign.moveUserToNode(request.getGwtUserId(), request.getOldNodeId(), request.getNewNodeId());
            log.info("转组返回====>{}",response);
            return JSONObject.parseObject(response, WorkFlowResponse.class);
        } catch (Exception e) {
            log.error("转移处理人失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "转移处理人失败");
        }
    }

    @Override
    public WorkFlowResponse resumeFlowInstance(ResumeFlowRequest request) {
        List<AfsGreatWallWorkflowNoticeException> afsGreatWallWorkflowNoticeExceptions = afsGreatWallWorkflowNoticeExceptionService
                .lambdaQuery()
                .eq(AfsGreatWallWorkflowNoticeException::getBizDataId,request.getBizDataId())
                .like(AfsGreatWallWorkflowNoticeException::getNoticeType, AfsEnumUtil.key(GwtMsgType.FLOW_EXCEPTION_NOTICE)+"%").list();
        String serviceId = null;
        for (AfsGreatWallWorkflowNoticeException exception : afsGreatWallWorkflowNoticeExceptions) {
            if(StringUtils.isNotEmpty(exception.getNoticeInfo())&&exception.getNoticeInfo().contains(request.getExceptionId())){
                serviceId = exception.getFlowServiceId();
                break;
            }
        }
        if (StringUtils.isNotEmpty(serviceId)) {
            if(afsGreatWallWorkflowBizDataService.exception(serviceId, request.getBizDataId())) {
                throw new AfsBaseException("流程业务端记录失败不允许恢复");
            }
        }
        try {
            String response = gwtWorkFLowFeign.resumeFlowInstance(request.getBizDataId(),request.getExceptionId());
            return JSONObject.parseObject(response, WorkFlowResponse.class);
        } catch (Exception e) {
            log.error("恢复流程实例失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "恢复流程实例失败");
        }
    }

    @Override
    public WorkFlowResponse pauseFlowInstance(PauseFlowInstance request) {
        try {
            String response = gwtWorkFLowFeign.pauseFlowInstance(request.getFlowInstanceId());
            log.info("暂停流程引擎返回=======>[{}]",response);
            return JSONObject.parseObject(response, WorkFlowResponse.class);
        } catch (Exception e) {
            log.error("暂停流程失败", e);
            throw new AfsBaseException(CommonConstants.FAIL, "暂停流程失败");
        }
    }
}
